package com.zkca.xf.ui.activity.technicalprotect;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.os.Bundle;
import android.view.View;

import com.google.gson.Gson;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zkca.xf.Constants_SP;
import com.zkca.xf.MyApplication;
import com.zkca.xf.R;
import com.zkca.xf.base.BaseActivity;
import com.zkca.xf.databinding.ActivityElectriGXBinding;

import com.zkca.xf.model.javabean.Dianliu_G;
import com.zkca.xf.model.javabean.uploadbean.RequestPage;
import com.zkca.xf.presenter.DianliuGPresenter;
import com.zkca.xf.utils.Arith;
import com.zkca.xf.utils.LogUtil;
import com.zkca.xf.utils.SPUtil;
import com.zkca.xf.utils.TimeUtils;
import com.zkca.xf.utils.ToastUtils;
import com.zkca.xf.view.FireAlarmLView;
import com.zkca.xf.view.OnClickView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.functions.Consumer;

public class ElectriG_XActivity extends BaseActivity implements OnClickView,FireAlarmLView {
    private String[] permissions = {Manifest.permission.CALL_PHONE};
    private String telNum;

    private DianliuGPresenter dianliuGPresenter;
    private List<Dianliu_G.DataBean> dataList=new ArrayList<>();
    private RequestPage requestPage;
    ActivityElectriGXBinding binding;
    Dianliu_G.DataBean dataBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_electri_g__x);
        binding = DataBindingUtil.setContentView(
                this, R.layout.activity_electri_g__x);
        Intent intent = getIntent();
        dianliuGPresenter=new DianliuGPresenter(this,this);
        boolean xg = intent.hasExtra("XG");
        if (xg) {
            int id = intent.getIntExtra("XG", 0);
            initResdata(id);

        } else {
            dataBean = (Dianliu_G.DataBean) intent.getSerializableExtra("dianhubean");


            bibdingdata();
        }

    }
    private void initResdata(int id) {
        Map map = new HashMap<String, Object>();
        map.put("id", id+"");
        requestPage = new RequestPage();
        requestPage.setAccessId(SPUtil.getString(this, Constants_SP.ACCESSID));
        requestPage.setPageNum(1);
        requestPage.setPageSize(10);
        requestPage.setParam(map);
        requestPage.setTimeStamp(TimeUtils.getTime());
        requestPage.setSign(MyApplication.getInstance().generateSign(requestPage));
        dianliuGPresenter.getDianG(requestPage);
    }

    private void bibdingdata() {

        binding.setDianliu(dataBean);
        binding.setCallback(this);
        long alarmTime = dataBean.getCreateTime();
        String formatDate2 = TimeUtils.getFormatDate2(alarmTime);
        telNum=dataBean.getTelphone();
        binding.alarmtime.setText(formatDate2);

        double value19 = dataBean.getValue19();
        double value20= dataBean.getValue20();
        double value21 = dataBean.getValue21();
        double value22 = dataBean.getValue22();
        LogUtil.e("123",value19+"");

        binding.value19.setText(value19+"");
        binding.value20.setText(value20+"");
        binding.value21.setText(value21+"");
        binding.value22.setText(value22+"");

        if (dataBean.getDevStatus()!=0){
            int  resultStatus =  dataBean.getResultStatus();
            if(0==resultStatus){
                binding.resultstatus.setText("待处理");
            }else if (1==resultStatus){
                binding.resultstatus.setText("真实");
                binding.resultstatus.setTextColor(Color.rgb(216, 29, 29));
            }else if (2==resultStatus){
                binding.resultstatus.setText("误报");
            }else if (3==resultStatus){
                binding.resultstatus.setText("故障");
            }else if (4==resultStatus){
                binding.resultstatus.setText("测试");
            }else if (5==resultStatus){
                binding.resultstatus.setText("忽略");
            }else if (6==resultStatus){
                binding.resultstatus.setText("已过期");
            }
        }else {
            binding.resultstatus.setText("--");
        }


        if (dataBean.getDevStatus() == 0) {
            binding.devstatus.setText("正常");

        } else if (dataBean.getDevStatus() == 1) {
            binding.devstatus.setText("预警");

        } else if (dataBean.getDevStatus() == 2) {
            binding.devstatus.setText("报警");

        } else if (dataBean.getDevStatus() == 3) {
            binding.devstatus.setText("断线");

        } else if (dataBean.getDevStatus() == 4) {
            binding.devstatus.setText("异常");

        } else if (dataBean.getDevStatus() == 5) {
            binding.devstatus.setText("故障");

        }

    }

    private void getNotescontact() {

        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setTitle("拨打电话");
        normalDialog.setMessage("电话号码：" + telNum);
        normalDialog.setPositiveButton("拨打",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        final Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + telNum));

                        RxPermissions rxPermissions = new RxPermissions(ElectriG_XActivity.this);
                        rxPermissions.request(permissions)
                                .subscribe(new Consumer<Boolean>() {
                                    @Override
                                    public void accept(@NonNull Boolean granted) throws Exception {
                                        //同意授权
                                        if (granted) {
                                            if (ActivityCompat.checkSelfPermission(ElectriG_XActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                                                ToastUtils.show(ElectriG_XActivity.this, "请添加手机拨打电话权限");
                                                return;
                                            }
                                            startActivity(intent);
                                        } else {//拒绝
                                            ToastUtils.show(ElectriG_XActivity.this, "请添加手机拨打电话权限");
                                        }
                                    }
                                });

                    }

                });
        normalDialog.setNegativeButton("返回",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        // 显示
        normalDialog.show();
    }

    @Override
    public void click1(View v) {
        switch (v.getId()) {
            case R.id.telphone:
                getNotescontact();
                break;
            case R.id.ll_basetitle_back:
                finish();
                break;
        }
    }

    @Override
    public void showProgressDialog() {

    }

    @Override
    public void hideProgressDialog() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void getFireAlarm(String s) {
        Gson gson = new Gson();
        Dianliu_G friealarmLbean = gson.fromJson(s, Dianliu_G.class);
        if (friealarmLbean.getResult() == 0) {
            List<Dianliu_G.DataBean> data = friealarmLbean.getData();
            if (data != null && data.size() > 0) {
                dataList.addAll(data);
                dataBean = dataList.get(0);
                bibdingdata();
            }
        } else {
            ToastUtils.show(this, friealarmLbean.getInfo());
        }
    }
}
